<?php 

class Ext_Utility_String 
{
    public static function random($length, $numeric = 0)
    {
        PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
        if($numeric) {
            $hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
        } else {
            $hash = '';
            $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
            $max = strlen($chars) - 1;
            for($i = 0; $i < $length; $i++) {
                $hash .= $chars[mt_rand(0, $max)];
            }
        }
        return $hash;
    }
    
    public static function dIconv($in_charset, $out_charset, $string)
    {
        if(strtoupper($in_charset) == strtoupper($out_charset))
        {
            return $string;
        }
        
        $return = '';
    
        if($string)
        {
            if(is_numeric($string))
            {
                $return = $string;
            }
            elseif(is_string($string))
            {
                if(function_exists('mb_convert_encoding'))
                {
                    $return = mb_convert_encoding($string, $out_charset, $in_charset);
                }
                elseif (function_exists('iconv'))
                {
                    $return = iconv($in_charset,$out_charset . (false!==strpos($out_charset,'/'.'/') ? '' : "/"."/TRANSLIT"), $string);
                }
            }
            else
            {
                $return = $string;
            }   
        }
    
        if(!$return)
        {
            $return = $string;
        }
    
        return $return;
    }
    
    public static function articleSummary($body, $size)
    {
        $_size = mb_strlen($body, 'utf-8');
        
        if($_size <= $size) return $body;
        
        $strlen_var = strlen($body);
        
        if(strpos($body, '<') === false){
            return mb_substr($body, 0, $size);
        }
        
        if($e = strpos($body, '<!-- break -->')){
            return mb_substr($body, 0, $e);
        }
        
        $html_tag = 0;
        
        $summary_string = '';
        
        $html_array = array('left' => array(), 'right' => array());
        for($i = 0; $i < $strlen_var; ++$i)
        {
            if(!$size){
                break;
            }
        
            $current_var = substr($body, $i, 1);
            
            if($current_var == '<'){
                $html_tag = 1;
                $htmlArrayStr = '';
            }else if($html_tag == 1){
                if($current_var == '>'){
                    $htmlArrayStr = trim($htmlArrayStr);
                    if(substr($htmlArrayStr, -1) != '/'){
                        $f = substr($htmlArrayStr, 0, 1);
                        if($f == '/'){
                            $html_array['right'][] = str_replace('/', '', $htmlArrayStr);
                        }else if($f != '?'){
                            if(strpos($htmlArrayStr, ' ') !== false){
                                $html_array['left'][] = strtolower(current(explode(' ', $htmlArrayStr, 2)));
                            }else{
                                $html_array['left'][] = strtolower($htmlArrayStr);
                            }
                        }
                    }

                    $htmlArrayStr = '';
                    $html_tag = 0;
                }else{
                    $htmlArrayStr .= $current_var;
                }
            }else{
                --$size;
            }
            
            $ord_var_c = ord($body{$i});
            
            switch (true) 
            {
                case (($ord_var_c & 0xE0) == 0xC0):
                $summary_string .= substr($body, $i, 2);
                $i += 1;
                break;
                case (($ord_var_c & 0xF0) == 0xE0):
                $summary_string .= substr($body, $i, 3);
                $i += 2;
                break;
                case (($ord_var_c & 0xF8) == 0xF0):
                $summary_string .= substr($body, $i, 4);
                $i += 3;
                break;
                case (($ord_var_c & 0xFC) == 0xF8):
                $summary_string .= substr($body, $i, 5);
                $i += 4;
                break;
                case (($ord_var_c & 0xFE) == 0xFC):
                $summary_string .= substr($body, $i, 6);
                $i += 5;
                break;
                default:
                $summary_string .= $current_var;
            }
        }

        if($html_array['left']){
            $html_array['left'] = array_reverse($html_array['left']);
            
            foreach($html_array['left'] as $index => $tag)
            {
                $key = array_search($tag, $html_array['right']);
                if($key !== false){
                    unset($html_array['right'][$key]);
                }else{
                    $summary_string .= '</'.$tag.'>';
                }
            }
        }
        return $summary_string;
    }
    
    public static function __filterData($_data)
    {
        $filter=new Zend_Filter_StripTags();
        if(is_array($_data))
        {
            foreach ($_data as $key=>$val)
            {
                if(is_array($val))
                {
                    self::filterData($val);
                }
                else 
                {
                    $_data[$key] = $filter->filter($val);
                }
            }
        }
        
        return $_data;
    }
    
    public static function filterTags($tags)
    {
        $tags = preg_replace("/\r\n/si", "\n", $tags);
        $tags = preg_replace("/\n+/si", ";", $tags);
        $tags = preg_replace("/；+/si", ";", $tags);
        $tags = preg_replace("/[;]+/si", ";", $tags);
        
        $_tagsArray = explode(";", $tags);
        $_tags = array();
        for($i=0;$i<count($_tagsArray);$i++){
            $_tagName = trim($_tagsArray[$i]);
            if(!$_tagName){
                continue;
            }
            if(in_array($_tagName, $_tags)){
                continue;
            }
            $_tags[] = $_tagName;
        }
        
        return join(';', $_tags);
    }
    
    public static function isBlogDomain($blogDomain)
    {
        if (!preg_match("/^[0-9a-z][0-9a-z-_]+[0-9a-z]$/si", $blogDomain)){
            return false;
        }

        if (strlen($blogDomain) > 63 || strlen($blogDomain) < 2){
            return false;
        }
        
        return true;
    }
    
    public static function escapeForJson($string)
    {
        return htmlentities($string, ENT_QUOTES);
    }
    
    
}